package com.jdd.modules.userstaff.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdd.common.api.vo.Result;
import com.jdd.common.aspect.annotation.AutoLog;
import com.jdd.common.system.query.QueryGenerator;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.common.util.excel.entity.params.ExportParams;
import com.jdd.common.util.excel.entity.params.ImportParams;
import com.jdd.common.util.excel.util.ExcelImportUtil;
import com.jdd.common.util.excel.web.entity.vo.NormalExcelConstants;
import com.jdd.common.util.excel.web.view.EasypoiSingleExcelView;
import com.jdd.dao.entity.TDepartment;
import com.jdd.dao.entity.THouse;
import com.jdd.dao.entity.TProjectRegion;
import com.jdd.dao.service.ITHouseService;
import com.jdd.dao.service.TDepartmentService;
import com.jdd.dao.service.TProjectRegionService;
import com.jdd.dao.service.TProjectService;
import com.jdd.feign.MqttFegin;
import com.jdd.modules.device.service.TDeviceService;
import com.jdd.modules.deviceLog.service.ITUserCmdLogService;
import com.jdd.modules.projectusertype.entity.ProjectUserType;
import com.jdd.modules.projectusertype.service.IProjectUserTypeService;
import com.jdd.modules.userstaff.dto.UserDto;
import com.jdd.modules.userstaff.dto.UserSearchDto;
import com.jdd.modules.userstaff.entity.CreateOrDelete;
import com.jdd.modules.userstaff.entity.User;
import com.jdd.modules.userstaff.mapper.UserMapper;
import com.jdd.modules.userstaff.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 人员
 * @Author: jdd
 * @Date: 2022-03-23
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "人员")
@RestController
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private TProjectService projectService;
    @Autowired
    private TProjectRegionService projectRegionService;
    @Autowired
    private IProjectUserTypeService projectUserTypeService;
    @Resource
    private TDepartmentService departmentService;
    @Resource
    private ITHouseService houseService;

    @Resource
    private TDeviceService deviceService;
    @Autowired
    private MqttFegin fegin;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ITUserCmdLogService userCmdLogService;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "人员-分页列表查询")
    @ApiOperation(value = "人员-分页列表查询", notes = "人员-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<UserDto>> queryPageList(UserSearchDto userSearchDto,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                String opTime_begin, String opTime_end,
                                                HttpServletRequest req) {
        Result<IPage<UserDto>> result = new Result<IPage<UserDto>>();
        Page<UserDto> page = new Page<>(pageNo, pageSize);
        UserDto userDto = new UserDto();
        List<String> projectCodes = new ArrayList<>();
        if (StringUtils.isNotBlank(userSearchDto.getProjectId())) {
            userDto.setProjectId(userSearchDto.getProjectId());
        } else {
            if (CollUtil.isNotEmpty(LoginUserUtils.getProjectCodeList())) {
                projectCodes = LoginUserUtils.getProjectCodeList();
            } else {
                return result;
            }
        }
        userDto.setUserName(userSearchDto.getUserName());
        userDto.setAreaId(userSearchDto.getAreaId());
        userDto.setStatus(userSearchDto.getStatus());
        Date createTimeBegin = null;
        Date createTimeEnd = null;
        if (StringUtils.isNotBlank(opTime_begin)&&!opTime_begin.equals("undefined 00:00:00")) {
            createTimeBegin = DateUtil.parse(opTime_begin, "yyyy-MM-dd HH:mm:ss");
        }
        if (StringUtils.isNotBlank(opTime_end)&&!opTime_end.equals("undefined 23:59:59")) {
            createTimeEnd = DateUtil.parse(opTime_end, "yyyy-MM-dd HH:mm:ss");
        }
        IPage<UserDto> ipage = userMapper.findProjectUserPage(page, userDto, projectCodes, createTimeBegin, createTimeEnd);

        result.setResult(ipage);
        result.setSuccess(true);
        return result;
    }

    @AutoLog(value = "查询人员绑定的所有的设备")
    @ApiOperation(value = "查询人员绑定的所有的设备", notes = "查询人员绑定的所有的设备")
    @GetMapping(value = "/getBindedDevice")
    public Result<List<String>> getBindedDevice(@RequestParam String userId) {
        Result<List<String>> result = new Result<>();
        List<String> deviceList = userService.getBindedDevice(userId);
        result.setSuccess(true);
        result.setResult(deviceList);
        return result;
    }

    @AutoLog(value = "查询人员绑定的所有的设备")
    @ApiOperation(value = "查询人员绑定的所有的设备", notes = "查询人员绑定的所有的设备")
    @GetMapping(value = "/getUserByProjectId")
    public Result<List<User>> getUserByProjectId(@RequestParam String id) {
        Result<List<User>> result = new Result<>();
        List<User> userList=new ArrayList<>();
        try {
       LambdaQueryWrapper<ProjectUserType> projectUserTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
       projectUserTypeLambdaQueryWrapper.eq(ProjectUserType::getProjectId,id);
       projectUserTypeLambdaQueryWrapper.eq(ProjectUserType::getTypeNum,"1");
        List<ProjectUserType> list = projectUserTypeService.list(projectUserTypeLambdaQueryWrapper);
        String typeId =null;
        if (CollectionUtils.isNotEmpty(list)){
            typeId = list.get(0).getId();
        }
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getProjectUserTypeId,typeId);
        userList = userService.list(userLambdaQueryWrapper);
        }catch (Exception e){
            log.error("根据项目查询业主接口错误",e);
            result.setSuccess(false);
            result.setMessage("查询错误");
            return result;
        }
        result.setResult(userList);
        return result;
    }





    /**
     * 查询项目下的区域
     *
     * @return
     */
    @AutoLog(value = "查询项目下的区域-查询")
    @ApiOperation(value = "查询项目下的区域-查询", notes = "查询项目下的区域-查询")
    @GetMapping(value = "/getAreaListByProjectId")
    public Result<List<TProjectRegion>> getAreaListByProjectId(@RequestParam String projectId) {
        Result<List<TProjectRegion>> result = new Result<>();
        LambdaQueryWrapper<TProjectRegion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TProjectRegion::getProjectId, projectId);
        List<TProjectRegion> list = projectRegionService.list(wrapper);
        result.setSuccess(true);
        result.setMessage("查询成功");
        result.setResult(list);
        return result;
    }


    /**
     * 查询项目下的部门
     *
     * @return
     */
    @AutoLog(value = "查询项目下的部门-查询")
    @ApiOperation(value = "查询项目下的部门-查询", notes = "查询项目下的部门-查询")
    @GetMapping(value = "/getDepartmentByProjectId")
    public Result<List<TDepartment>> getDepartmentByProjectId(@RequestParam String projectId) {
        Result<List<TDepartment>> result = new Result<>();
        LambdaQueryWrapper<TDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TDepartment::getProjectId, projectId);
        List<TDepartment> list = departmentService.list(wrapper);
        result.setSuccess(true);
        result.setMessage("查询成功");
        result.setResult(list);
        return result;
    }

    /**
     * 查询项目下的人员类型
     *
     * @return
     */
    @AutoLog(value = "查询项目下的人员类型-查询")
    @ApiOperation(value = "查询项目下的部门-查询", notes = "查询项目下的人员类型-查询")
    @GetMapping(value = "/getHousetByFloorId")
    public Result<List<THouse>> getHousetByFloorId(@RequestParam String floorId) {
        Result<List<THouse>> result = new Result<>();
        LambdaQueryWrapper<THouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(THouse::getFloorId, floorId);
        List<THouse> list = houseService.list(wrapper);
        result.setSuccess(true);
        result.setMessage("查询成功");
        result.setResult(list);
        return result;
    }

    /**
     * 查询项目下的人员类型
     *
     * @return
     */
    @AutoLog(value = "查询项目下的人员类型-查询")
    @ApiOperation(value = "查询项目下的部门-查询", notes = "查询项目下的人员类型-查询")
    @GetMapping(value = "/getProUserTypeListByProId")
    public Result<List<ProjectUserType>> getProUserTypeListByProId(@RequestParam String projectId) {
        Result<List<ProjectUserType>> result = new Result<>();
        LambdaQueryWrapper<ProjectUserType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProjectUserType::getProjectId, projectId);
        List<ProjectUserType> list = projectUserTypeService.list(wrapper);
        result.setSuccess(true);
        result.setMessage("查询成功");
        result.setResult(list);
        return result;
    }

    /**
     * 添加
     *
     * @param
     * @return
     */
    @AutoLog(value = "人员-添加")
    @ApiOperation(value = "人员-添加", notes = "人员-添加")
    @PostMapping(value = "/add")
    public Result<UserDto> add(HttpServletRequest req) throws IOException {
        Result<UserDto> result = new Result<>();
        String faceUrl=null;
        String diffStrs=null;
        try {
         faceUrl  = userService.uploadImgWithBase64(req);
            diffStrs = userService.caseHttpRequst2String(req);
            diffStrs+=","+"null"+"->"+faceUrl;
        }catch (Exception e){
            log.error("人员添加转换图片数据发生错误", e);
            result.setSuccess(false);
            result.setMessage("人员添加转换图片数据发生错误");
            return result;
        }
        User user = null;
        try {
            user = userService.encaUserwithServletRequset(req, faceUrl);
        } catch (Exception e) {
            log.error("封装人员数据错误",e);
           result.setMessage("封装人员数据出错");
           result.setSuccess(false);
           return result;
        }
        try {
            /*判断该用户新增时是否为户主，户主编号为"1",且不允许修改*/
            if (user.getProjectUserTypeId()!=null){
                ProjectUserType projectUserType = projectUserTypeService.getById(user.getProjectUserTypeId());
                if (projectUserType.getTypeNum().equals("1")){
                    if (user.getHouseId()!=null && !user.getHouseId().equals("")){
                        THouse tHouse = houseService.getById(user.getHouseId());
                        if (tHouse!=null){
                            if (StringUtils.isEmpty(tHouse.getHostId())){
                                tHouse.setHostId(user.getId());
                                houseService.saveOrUpdate(tHouse);
                            }else{
                                result.setMessage("一个房间只能有一个户主");
                                result.setSuccess(false);
                                return result;
                            }
                        }
                    }else {
                        result.setMessage("人员必须绑定到房间才能是户主");
                        result.setSuccess(false);
                        return result;
                    }
                }
            }
            userService.save(user);

        } catch (Exception e) {
            log.error("储存用户实体错误->{}", e);
            result.setMessage("储存用户实体错误");
            result.setSuccess(false);
            return result;
        }
        String uid = user.getId();
        try {
            log.info("批量创建人脸：开始进行uid为{}的人脸创建", uid);
            List<String> deivces = userService.QueryUserInitialEquipment(uid);
            log.info("用户{}需要绑定的设备有{}",uid,deivces);
            userService.createUser(uid, deivces,null,1);
            log.info("批量创建人脸：uid为{}的人脸创建成功", uid);
        } catch (Exception e) {
            log.error("批量创建人脸：uid为{}的人脸信息到机器失败,原因为->{}", uid, e);
            return Result.error("下发人脸信息到机器失败");
        }
        result.setMessage("指令下发成功，待生效");
        return result;
    }


    @AutoLog(value = "人员单独授权接口")
    @ApiOperation(value = "人员单独授权接口", notes = "人员单独授权接口")
    @PostMapping(value = "/createOrDelete")
    public Result<UserDto> createOrDelete(@RequestBody CreateOrDelete createOrDelete) {
        Result<UserDto> result = new Result<>();
        if (createOrDelete != null) {
            String userId = createOrDelete.getUserId();
            String insertDeviceStr = createOrDelete.getInsertDeviceStr();
            String removeDeviceStr = createOrDelete.getRemoveDeviceStr();
            User user = userService.getById(userId);
            if (StringUtils.isNotBlank(insertDeviceStr)) {
                String[] split = insertDeviceStr.split(",");
                List<String> deviceList = Arrays.asList(split).stream().distinct().collect(Collectors.toList());
                log.info("人员单独创建用户id" + userId);
                log.info("人员单独创建sn：" + insertDeviceStr);
                if (CollectionUtils.isEmpty(deviceList)) {
                    log.error("人员{}单独授权新增设备为空", userId);
                    result.setMessage("人员单独授权新增设备为空");
                    result.setSuccess(false);
                    return result;
                }
                List<String> oldDeviceList = userService.getBindedDevice(userId);
                try {
                    String diffStr ="绑定设备："+oldDeviceList.toString()+"->"+ deviceList.toString();
                    userService.createUser(userId, deviceList,diffStr,6);
                } catch (Exception e) {
                    log.error("创建单个人脸信息发送指令模块失败", e);
                    return Result.error("创建单个人脸信息发送指令模块失败");
                }
            } else {
                log.info("人员单独授权传入的新增设备的sn为空");
            }
            if (StringUtils.isNotBlank(removeDeviceStr)) {
                String[] split = removeDeviceStr.split(",");
                List<String> deviceList = Arrays.asList(split).stream().distinct().collect(Collectors.toList());
                if (CollectionUtils.isEmpty(deviceList)) {
                    log.error("人员{}单独授权删除设备为空", userId);
                    result.setMessage("人员单独授权删除设备为空");
                    result.setSuccess(false);
                    return result;
                }
                try {
                    userService.deleteUser(deviceList, userId, 0, user.getProjectId(),6);
                } catch (Exception e) {
                    log.error("单个儿删除人脸信息失败，原因为->", e);
                    return Result.error("单个儿删除人脸信息失败");
                }
            } else {
                log.info("人员单独授权传入的删除设备的sn为空");
            }
        } else {
            result.setSuccess(false);
            result.setMessage("传入的参数为空");
            log.error("传入的参数为空");
            return result;
        }
        return result;
    }



    /**
     * 编辑
     *
     * @param req
     * @return
     */
    @AutoLog(value = "人员-编辑")
    @ApiOperation(value = "人员-编辑", notes = "人员-编辑")
    @PostMapping(value = "/edit")
    public Result<UserDto> edit(HttpServletRequest req) {
        Result<UserDto> result = new Result<>();
        String faceUrl=null;
        User user=new User();
        User oldUser=null;
        String diffStrs=null;
        try {
           faceUrl  = userService.uploadImgWithBase64(req);
           user = userService.encaUserwithServletRequset(req, faceUrl);
          diffStrs = userService.caseHttpRequst2String(req);
        }catch (Exception e){
            log.error("编辑人员保存信息出错",e);
            result.setSuccess(false);
            result.setMessage("编辑人员保存信息出错");
            return result;
        }
        String userId = user.getId();
        oldUser = userService.getById(userId);
        if (faceUrl!=null){
            diffStrs+=","+oldUser.getImgUrl()+"->"+faceUrl;
        }

        List<String> oldBindDevice = userService.getBindedDevice(user.getId());
        try {
            /*先判断是否类型是否发生改变，且变化前是不是户主，变化后是不是户主*/
            if (oldUser.getProjectUserTypeId()!=user.getProjectUserTypeId()){
                /*判断编辑前是不是户主*/
                ProjectUserType oldUserType = projectUserTypeService.getById(oldUser.getProjectUserTypeId());
                /*如果编辑前是户主进行解绑*/
                if (oldUserType!=null&&"1".equals(oldUserType.getTypeNum())) {
                    if (StringUtils.isNotEmpty(oldUser.getHouseId())) {
                        LambdaUpdateWrapper<THouse> tHouseLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                        tHouseLambdaUpdateWrapper.eq(THouse::getId, oldUser.getHouseId());
                        tHouseLambdaUpdateWrapper.set(THouse::getHostId, null);
                        houseService.update(tHouseLambdaUpdateWrapper);
                    }
                }
                /*判断该用户编辑后为户主，户主编号为"1",且不允许修改,*/
                if (user.getProjectUserTypeId()!=null){
                    ProjectUserType projectUserType = projectUserTypeService.getById(user.getProjectUserTypeId());
                    if (projectUserType!=null) {
                        if ("1".equals(projectUserType.getTypeNum())){
                            if (user.getHouseId() != null && !user.getHouseId().equals("")) {
                                THouse tHouse = houseService.getById(user.getHouseId());
                                if (StringUtils.isNotEmpty(tHouse.getHostId())) {
                                    result.setMessage("该房间已绑定有户主，请确认后再绑定！");
                                    result.setSuccess(false);
                                    return result;
                                }
                                tHouse.setHostId(user.getId());
                                houseService.saveOrUpdate(tHouse);
                            } else {
                                result.setMessage("人员必须绑定到房间才能是户主");
                                result.setSuccess(false);
                                return result;
                            }
                        }
                    }
                }
            }

            userService.saveOrUpdate(user);
        } catch (Exception e) {
            log.error("编辑保存实体失败", e);
            result.setMessage("编辑保存实体失败");
            result.setSuccess(false);
        }

            try {
                userService.updateUser(userId, oldBindDevice, diffStrs);
            } catch (Exception e) {
                log.error("编辑下发失败", e);
                result.setSuccess(false);
                result.setMessage("编辑下发失败");
                return result;
            }

        result.setMessage("指令下发成功，待生效");
        return result;
    }

    /**
     * 通过id删除
     *
     * @param userId
     * @return
     */
    @AutoLog(value = "人员-通过id删除")
    @ApiOperation(value = "人员-通过id删除", notes = "人员-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String userId) {

        User user = userService.getById(userId);
        if (user != null) {
            List<String> deviceList = userService.getBindedDevice(userId);
            log.info("要删除人脸信息的设备有->{}", deviceList);
            try {
                userService.deleteUser(deviceList, userId, 0, user.getProjectId(),3);
                judgeBind(user);
                userService.removeById(userId);
            } catch (Exception e) {
                log.error("单个删除人脸指令创建或发送错误-{}", e);
                return Result.error("单个删除人脸指令创建或发送错误");
            }
        }
        return Result.ok("删除中！");
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "人员-批量删除")
    @ApiOperation(value = "人员-批量删除", notes = "人员-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<User> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        Result<User> result = new Result<User>();
        if (ids == null || "".equals(ids.trim())) {
            return result.error500("参数不识别！");
        } else {
            for (String userId : Arrays.asList(ids.split(","))) {
                User user = userService.getById(userId);
                List<String> bindedDevice = userService.getBindedDevice(userId);
                if (CollectionUtils.isEmpty(bindedDevice)){
                    log.info("用户绑定设备为空，删除用户实体");
                    if (user!=null){
                        judgeBind(user);
                    }
                    userService.removeById(userId);
                    result.setMessage("删除成功");
                    result.setSuccess(true);
                    return result;
                }

                try {
                    judgeBind(user);
                    userService.deleteUser(bindedDevice, userId, 0, user.getProjectId(),3);

                } catch (Exception e) {
                    log.error("批量删除指令创建或发送错误->", e);
                    return result.error500("批量删除指令创建或发送错误！");
                }
            }
        }
        result.success("删除中，待生效！");
        return result;
    }

    private void judgeBind(User user) {
        if (StringUtils.isNotEmpty(user.getProjectUserTypeId())) {
            /*先判断绑定关系*/
            ProjectUserType oldUserType = projectUserTypeService.getById(user.getProjectUserTypeId());
            /*如果编辑前是户主进行解绑*/
            if (oldUserType!=null){
            if ("1".equals(oldUserType.getTypeNum())) {
                if (StringUtils.isNotEmpty(user.getHouseId())) {
                    LambdaUpdateWrapper<THouse> tHouseLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                    tHouseLambdaUpdateWrapper.eq(THouse::getId, user.getHouseId());
                    tHouseLambdaUpdateWrapper.set(THouse::getHostId, null);
                    houseService.update(tHouseLambdaUpdateWrapper);
                }
            }
            }
        }
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "人员-通过id查询")
    @ApiOperation(value = "人员-通过id查询", notes = "人员-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<User> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<User> result = new Result<User>();
        User user = userService.getById(id);
        if (user == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(user);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 导出excel
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response, User user) {
        // Step.1 组装查询条件
        QueryWrapper<User> queryWrapper = QueryGenerator.initQueryWrapper(user, request.getParameterMap());

        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new EasypoiSingleExcelView());
        List<User> pageList = userService.list(queryWrapper);
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "人员列表");
        mv.addObject(NormalExcelConstants.CLASS, User.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("人员列表数据", "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = ImportParams.builder().build();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<User> listUsers = ExcelImportUtil.importExcel(file.getInputStream(), User.class, params);
                userService.saveBatch(listUsers);
                return Result.ok("文件导入成功！数据行数:" + listUsers.size());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.ok("文件导入失败！");
    }

}
